package Controler;

import Module.User;
import Module.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-11-23
 * Time: 20:06
 * To BE A Better Person-2020-3-2
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    /**
     * 登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.setCharacterEncoding("utf8");
        String username=req.getParameter("username");
        String password=req.getParameter("password");

        //输入的账号密码不符合
        if(username==""||username==null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("请输入正确格式");
            resp.setHeader("Location","login.html");
            return;
        }
        if(password==""||password==null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("请输入正确格式");
            resp.setHeader("Location","login.html");
            return;
        }
        //从数据库中提取用户信息
        UserDao userDao=new UserDao();
        //账号密码不正确
        User user = userDao.selectByname(username);
        if(user == null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("请输入正确的账号密码");
            resp.setHeader("Location","login.html");
            return;
        }
        System.out.println(user);
        if(!user.getUsername().equals(username)){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("请输入正确的账号密码");
            resp.setHeader("Location","login.html");
            return;
        }
        if(!user.getPassword().equals(password)){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("请输入正确的账号密码");
            resp.setHeader("Location","login.html");
            return;
        }
        //账号密码都正确就存session
        HttpSession session= req.getSession();
        session.setAttribute("user",user);
        System.out.println(req.getHeaderNames().toString());
        resp.setStatus(302);
        resp.setHeader("Location","blog_list.html");
        System.out.println(req.getHeaderNames().toString());
    }

    /**
     * 强制登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session= req.getSession(false);
        if(session == null){
            resp.setContentType("text/html;charset-utf8");
            resp.setStatus(403);
            resp.getWriter().write("请完成登录操作");
            return;
        }
        User user= (User) session.getAttribute("user");
        if(user == null){
            resp.setContentType("text/html;charset-utf8");
            resp.setStatus(403);
            resp.getWriter().write("请完成登录操作");
            return;
        }
        resp.setStatus(200);
    }
}
